/**
 *  @fileOverview  UUcode module
 */

;(function() {

'use strict';

var UUcode = {

	generalDeRegExp : /#\w+?#/,
	supportMsg : '',
	codeInsert : function() {
		'use strict';
		var editarea = $( ".tb-editor-editarea" );
		if ( editarea.text().match( UUcode.generalDeRegExp ) )
			return;
		var input = prompt( UUcode.supportMsg );
		if ( !input )
			return;

		var key = '';
		for ( var patternName in UUcode.Pattern ) {
			if ( UUcode.Pattern[patternName].enRegExp.test( input ) ) {
				key = RegExp.$1;
				editarea.html( editarea.html() + '#' + patternName + '#' + key + ' ' );
				break;
			}
		}
	},
	Decode : function( content, patternName ) {
		'use strict';
		patternName = patternName.toLowerCase();
		var childNodes = content.childNodes;
		var pattern = this.Pattern[patternName];
		var id, found = false;
		var i = 0, l = childNodes.length;
		for ( ; i < l; i++ ) {
			if ( pattern.deRegExp.test( childNodes[i].textContent ) ) {
				id = RegExp.$1;
				found = true;
				break;
			}
		}
		if ( !found )
			return;
		var wrapper = $( '<section>' );
		var title = $( '<span>' ).addClass( 'uucode-title' ).text( '#' + pattern.title + '#' + id ).attr( 'title', '点击展开内容' );
		title[0].style.color = "Green";
		var mainElem = $( '<'+pattern.mainElementTagName+'>' ).attr( pattern.generateAttributes(id) ).addClass( 'uucode-widget' );
		wrapper.append( title );
		title.click( function() {
			if ( !title.siblings().length ) {
				wrapper.append(mainElem);
				title.attr( 'title', '点击关闭内容' );
			} else {
				mainElem.remove();
				title.attr( 'title', '点击展开内容' );
			}
		});
		content.replaceChild( wrapper[0], childNodes[i] );
	},
	Pattern : {
		__proto__ : null,
		utb : {
			title : 'Youtube',
			enRegExp : /youtu\.?be.*?\/(?:watch\?v=)?([A-Za-z0-9_-]{11})/,
			deRegExp : /#UTB#\W*([A-Za-z0-9_-]{11})/i,
			mainElementTagName : 'iframe',
			generateAttributes : function( id ) {
				return {
					'class' : 'youtube',
					width : '95%',
					height : '450',
					src : "https://www.youtube.com/embed/" + id +'?theme=dark&color=red'
				}
			}
		},
		bilibili : {
			title : 'Bilibili',
			enRegExp : /(?:bilibili\.tv.+av|loli\.my.*aid=)(\d+)/,
			deRegExp : /#bilibili#\W*(?:av)??(\d+)/i,
			mainElementTagName : 'embed',
			generateAttributes : function( id ) {
				return {
					'class' : 'bilibili',
					width : '544',
					height : '452',
					src : "http://static.loli.my/miniloader.swf",
					type : "application/x-shockwave-flash",
					allowfullscreen : "true",
					pluginspage : "http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash",
					flashvars : 'aid=' + id
				}
			}
		},
		slideshare : {
			title : 'SlideShare',
			enRegExp : /slideshare\.net\/slideshow\/embed_code\/(\d+)/,
			deRegExp : /#SlideShare#\W*(\d+)/i,
			mainElementTagName : 'iframe',
			generateAttributes : function( id ) {
				return {
					width : '595',
					height : '497',
					src : 'http://www.slideshare.net/slideshow/embed_code/' + id
				}
			}
		},
		soundcloud : {
			title : 'SoundCloud',
			enRegExp : /soundcloud.*((?:tracks|playlists)%2F(?:\d+))/,
			deRegExp : /#SoundCloud#\W*((?:tracks|playlists)%2F(?:\d+))/i,
			mainElementTagName : 'iframe',
			generateAttributes : function( id ) {
				return {
					width : '100%',
					height : '166',
					src : 'http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2F' +
							id + '&amp;auto_play=false&amp;show_artwork=true&amp;color=ff7700'
				}
			}
		}
		/* vimeo : banned */

	}

};

var _keys = [], _titles = [];
for ( var k in UUcode.Pattern ) {
	_keys.push(k);
	_titles.push( UUcode.Pattern[k]['title'] );
}
UUcode.generalDeRegExp = new RegExp( '#(' + _keys.join('|') + ')#', 'i' );
UUcode.supportMsg = '目前支持 ' + _titles.join( '; ' );


window.UUcode = UUcode;


})();
